//
//  SimpleChart.m
//  SampleHeartRateApp
//
//  Created by Chris Greening on 25/11/2010.
//  Copyright 2010 CMG Research. All rights reserved.
//

#import "SimpleChart.h"


@implementation SimpleChart

@synthesize points;
@synthesize delegate;


- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
		if(points.count==0) return;
		// Drawing code.
		CGContextRef context=UIGraphicsGetCurrentContext();
		CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
		CGContextSetLineWidth(context, 1);
		CGContextBeginPath(context);
		float xpos=self.bounds.size.width;
		float ypos=[[points objectAtIndex:0] floatValue];
		CGContextMoveToPoint(context, xpos, ypos);
		for(int i=1; i<points.count; i++) {
			xpos-=2;
			float ypos=[[points objectAtIndex:i] floatValue];
			CGContextAddLineToPoint(context, xpos, self.bounds.size.height/2+ypos*self.bounds.size.height/2);
		}
		CGContextStrokePath(context);
}

-(void)fingerCoverLEDFlashHandle{
    
}

-(void) addPoint:(NSArray *) array {
	if(!points) points=[[NSMutableArray alloc] init];
    if (finishDetection) {
        return;
    }
    if ([[array objectAtIndex:2] floatValue] >0.7 || [[array objectAtIndex:1] floatValue] < 0.3 || [[array objectAtIndex:3] floatValue] > 0.4) {
        rateCountLabel.text = @"确保手指完全覆盖LED闪光灯和摄像头";
        [points removeAllObjects];
        startDate = 0;
        endDate = 0;
        changeCount = 0;
        rateCount = 0;
        finishDetection = NO;
        return;
    }
    if (!startDate) {
        startDate = [[NSDate date] timeIntervalSince1970];
    }else{
        if ([[NSDate date] timeIntervalSince1970] - startDate < 6) {
            rateCountLabel.text = @"正在检测,请稍后...";
            return;
        }
    }
	[points insertObject:[array objectAtIndex:0] atIndex:0];
	while(points.count>self.bounds.size.width/2) {
		[points removeLastObject];
	}
    self.backgroundColor = [UIColor colorWithRed:[[array objectAtIndex:1] floatValue] green:[[array objectAtIndex:2] floatValue] blue:[[array objectAtIndex:3] floatValue] alpha:1];
    if (points.count >= 2) {
        if ([[points objectAtIndex:0] floatValue] > 0 && [[points objectAtIndex:1] floatValue] < 0) {
            //进入正值区域
            changeCount++;
            meddleCount = 0;
        }else if ([[points objectAtIndex:0] floatValue] < 0 && [[points objectAtIndex:1] floatValue] > 0){
            //进入负值区域
            if (meddleCount > 2) {
                changeCount++;
            }else{
                changeCount = 0;
            }
            meddleCount = 0;
        }else{
            meddleCount++;
        }
        if (changeCount == 2) {
            rateCount++;
            changeCount = 0;
            if (rateCount == 1) {
                endDate = [[NSDate date] timeIntervalSince1970];
            }else if (rateCount == 20){
                rateCount = (NSInteger)60.0/(([[NSDate date] timeIntervalSince1970] - endDate) / 19);
                if (rateCount > 100 || rateCount < 50) {
                    rateCountLabel.text = @"测试失败，重测！";
                }else{
                    rateCountLabel.text = [NSString stringWithFormat:@"心跳/M：%d",rateCount];
                }
                finishDetection = YES;
                if ([delegate respondsToSelector:@selector(detectionFinishNotif)]) {
                    [delegate detectionFinishNotif];
                }
                return;
            }
//            else{
//                if ([[NSDate date] timeIntervalSince1970] - endDate >= 20) {
//                    isStop = YES;
//                    rateCount = 3*rateCount;
//                    if (rateCount > 160 || rateCount < 50) {
//                        rateCountLabel.text = @"测试失败，重测！";
//                    }else{
//                        rateCountLabel.text = [NSString stringWithFormat:@"心跳/M：%d",rateCount];
//                    }
//                }
//            }
        }
        rateCountLabel.text = [NSString stringWithFormat:@"心跳：%d",rateCount];
    }
	[self setNeedsDisplay];
}

-(void)clearDetectionData{
    [points removeAllObjects];
    startDate = 0;
    endDate = 0;
    changeCount = 0;
    rateCount = 0;
    finishDetection = NO;
}

- (void)dealloc {
	self.points = nil;
}

@end
